<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="房屋名字" prop="name">
      <el-input v-model="dataForm.name" placeholder="房屋名字"></el-input>
    </el-form-item>
    <el-form-item label="户主id" prop="tenantId">
      <el-input v-model="dataForm.tenantId" placeholder="户主id"></el-input>
    </el-form-item>
    <el-form-item label="租房者id" prop="renterId">
      <el-input v-model="dataForm.renterId" placeholder="租房者id"></el-input>
    </el-form-item>
    <el-form-item label="房屋所在地" prop="location">
      <el-input v-model="dataForm.location" placeholder="房屋所在地"></el-input>
    </el-form-item>
    <el-form-item label="房屋面积" prop="area">
      <el-input v-model="dataForm.area" placeholder="房屋面积"></el-input>
    </el-form-item>
    <el-form-item label="房屋价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="房屋价格"></el-input>
    </el-form-item>
    <el-form-item label="启用状态[0 - 不出租，1 - 在售,2 - 已售]" prop="status">
      <el-input v-model="dataForm.status" placeholder="启用状态[0 - 不出租，1 - 在售,2 - 已售]"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          tenantId: '',
          renterId: '',
          location: '',
          area: '',
          price: '',
          status: ''
        },
        dataRule: {
          name: [
            { required: true, message: '房屋名字不能为空', trigger: 'blur' }
          ],
          tenantId: [
            { required: true, message: '户主id不能为空', trigger: 'blur' }
          ],
          renterId: [
            { required: true, message: '租房者id不能为空', trigger: 'blur' }
          ],
          location: [
            { required: true, message: '房屋所在地不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '房屋面积不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '房屋价格不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '启用状态[0 - 不出租，1 - 在售,2 - 已售]不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/rentHouse/house/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.house.name
                this.dataForm.tenantId = data.house.tenantId
                this.dataForm.renterId = data.house.renterId
                this.dataForm.location = data.house.location
                this.dataForm.area = data.house.area
                this.dataForm.price = data.house.price
                this.dataForm.status = data.house.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/rentHouse/house/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'tenantId': this.dataForm.tenantId,
                'renterId': this.dataForm.renterId,
                'location': this.dataForm.location,
                'area': this.dataForm.area,
                'price': this.dataForm.price,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
